from extension.ext_sql import db
from models.demo import Person

class DemoService:

    @staticmethod
    def add_person(person: Person):
        """
        增加
        :param person: 对象 -> Person
        :return: None
        """
        result = db.session.add(person)
        db.session.commit()

        return result

    @staticmethod
    def del_person_by_id(person_id):
        """
        删除
        :param person_id: 对象 id -> int
        :return: 被删除的数量 -> int
        """
        result = Person.query.filter(Person.id == person_id).delete()
        db.session.commit()
        return result

    @staticmethod
    def update_person(person_dict):
        """
        修改: 传入字典，根据字典中的键值修改 Person 表中的记录
        :param person_dict: Person 对象的字典
        """
        # result = Person.query.filter(Person.id == person_id).update({"name":Person.name + 'str'})
        result = Person.query.filter(Person.id == person_dict['id']).update(person_dict)
        db.session.commit()
        return result

    @staticmethod
    def query_person_by_id(person_id) -> Person:
        """
        查询
        :param person_id: 唯一 id
        """
        stmt = db.select(Person).where(Person.id == person_id)
        result = db.session.execute(stmt).scalar()

        return result